<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use DB;
class RegController extends Controller{
   //申请代理商页面
    public function getIndex(){
        $allAgent = AgentController::allAgent(0);
        foreach ($allAgent as $k=>$v){
            $allAgent[$k]['agent_name'] = str_repeat('--',substr_count($v['path'],',')-1).$v['agent_name'];
        }
        return view('admin.reg.index',['allAgent'=>$allAgent]);
    }

    //提交审批处理
    public function postApprove(Request $request){
        //获取ajax数据
        $data = $request -> except('_token');
        //查询申请代理商表
        $row = DB::table('sa_approve') -> where(['account'=>$data['account']]) -> first();
        if($row){
            $arr['status'] = '-2';
            $arr['msg'] = '账号已存在';
        }else{
            //插入申请代理商表
            $data['password'] = Hash::make($data['password']);
            //unset($data['verify']);
            $insertId = DB::table('sa_approve') -> insertGetId($data);
            if($insertId){
                $arr['status'] = '1';
                $arr['msg'] = '你已成功发出申请，请等待商家审批';
            }else{
                $arr['status'] = '-1';
                $arr['msg'] = '您发出申请失败';
            }
        }
        echo json_encode($arr);
    }

    //验证账号是否已存在
    public function postTest(Request $request){
        $account = $request -> input('account');

        //查询申请代理商表
        $row = DB::table('sa_approve') -> where(['account'=>$account]) -> first();
        if($row){
            $data['status'] = '-1';
            $data['msg'] = '账号已存在';
        }else{
            $data['status'] = '1';
        }
        echo json_encode($data);
    }
}
